package com.hongbo.echartsjavawebdemo.echarts.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.abel533.echarts.*;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Position;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
import com.github.abel533.echarts.series.Series;
import com.github.abel533.echarts.style.ItemStyle;
import com.github.abel533.echarts.style.NameTextStyle;
import com.github.abel533.echarts.style.itemstyle.Normal;
import com.hongbo.echartsjavawebdemo.echarts.service.IEchartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.util.*;

@Slf4j
@Service
public class EchartServiceImpl implements IEchartService {


    public Option selectRemoveCauses() {

        // 查询前20
        // 数据库查询获取统计数据
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (int i = 0; i < 10; i++) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("NAME", "张三" + i);
            map.put("TOTAL", new Random().nextInt(50));
            list.add(map);

        }

        // 创建Option
        Option option = new Option();
        option.title("剔除药品").tooltip(Trigger.axis).legend("金额（元）");

        // 横轴为值轴
        option.xAxis(new ValueAxis().boundaryGap(0d, 0.01));

        // 创建类目轴
        CategoryAxis category = new CategoryAxis();

        // 柱状数据
        Bar bar = new Bar("金额（元）");

        // 饼图数据
        Pie pie = new Pie("金额（元）");

        // 循环数据
        for (Map<String, Object> objectMap : list) {

            // 设置类目
            category.data(objectMap.get("NAME"));
            System.out.println(objectMap.get("NAME"));

            // 类目对应的柱状图
            bar.data(objectMap.get("TOTAL"));

            // 饼图数据
            System.out.println(objectMap.get("TOTAL"));
            pie.data(new PieData(objectMap.get("NAME").toString(), objectMap.get("TOTAL")));

        }

        // 设置类目轴
        option.yAxis(category);

        // 饼图的圆心和半径
        pie.center(900, 380).radius(100);

        // 设置数据
        option.series(bar, pie);

        // 由于药品名字过长，图表距离左侧距离设置180，关于grid可以看ECharts的官方文档
        option.grid().x(180);

        // 返回Option
        return option;

    }

    public Option selectSelling() {

        // 查询前20
        // 数据库查询获取统计数据
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] str = new String[]{"衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子","皮鞋","七分裤"};
//		String[] str = new String[]{"1","2","3","4","5","6","7","8"};

        for (int i = 0; i < str.length; i++) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("NAME", str[i]);
            map.put("TOTAL", new Random().nextInt(100));
            list.add(map);

        }

        // 创建Option
        Option option = new Option();
        List<String> legendList = new ArrayList<String>();
        legendList.add("销量");
//		option.title("剔除药品").tooltip(Trigger.axis).legend("金额（元）");
        option.title("ECharts 测试").tooltip(Trigger.axis).legend().setData(legendList);;


        // 创建类目轴
        CategoryAxis category = new CategoryAxis();

        // 柱状数据
//		Bar bar = new Bar("销量");
        Bar bar = new Bar(legendList.get(0));

        // 饼图数据
//		Pie pie = new Pie("销量");
//		Pie pie = new Pie(legendList.get(0));

        // 循环数据
        for (Map<String, Object> objectMap : list) {

            // 设置类目
            category.data(objectMap.get("NAME"));
            System.out.println(objectMap.get("NAME"));

            // 类目对应的柱状图
            bar.data(objectMap.get("TOTAL"));

            // 饼图数据
            System.out.println(objectMap.get("TOTAL"));
//			pie.data(new PieData(objectMap.get("NAME").toString(), objectMap.get("TOTAL")));

        }

//		设置横轴为值轴
        option.xAxis(category);
//		设置纵轴为类目轴
        option.yAxis(new ValueAxis().boundaryGap(0d, 0.01));

        //设置横为值轴
//		option.xAxis(new ValueAxis().boundaryGap(0d, 0.01));
//		// 纵轴为类目
//		option.yAxis(category);


        // 饼图的圆心和半径
//		pie.center(900, 380).radius(100);

        // 设置数据
//		option.series(bar, pie);
        option.series(bar);

        // 由于药品名字过长，图表距离左侧距离设置180，关于grid可以看ECharts的官方文档
        option.grid().x(40);

        // 返回Option
        return option;

    }

    public Option weekLoginCount() {


        // 数据库查询获取统计数据
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] str = new String[]{"2018/01/21","2018/01/28","2018/02/04","2018/02/11","2018/02/18","2018/02/25","2018/03/04","2018/03/11"};

        for (int i = 0; i < str.length; i++) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("WEEK", str[i]);
            map.put("TOTAL", new Random().nextInt(1000));
            list.add(map);

        }


        // 创建Option
        Option option = new Option();
        List<String> legendList = new ArrayList<String>();
        legendList.add("人次");
        legendList.add("趋势");
        option.title("每周登录人次").tooltip(Trigger.axis).legend().setData(legendList);;


        // 创建类目轴
        CategoryAxis category = new CategoryAxis();

//        // 柱状数据
        Bar bar = new Bar(legendList.get(0));
        bar.setName(legendList.get(0));
        bar.setType(SeriesType.bar);

        ItemStyle itemStyle = new ItemStyle();
        Normal normal = new Normal();
        Label label = new Label();
        label.setShow(true);
        label.setPosition(Position.top);

//        normal.setLabel(label);
//        itemStyle.setNormal(normal);
//        bar.setItemStyle(itemStyle);

        //等价写法
        bar.itemStyle().normal().label(label);

        Line line = new Line();
        line.setName(legendList.get(1));
        line.setType(SeriesType.line);
//        line.setLabel(label);


        // 循环数据
        for (Map<String, Object> objectMap : list) {
            // 设置类目
            category.data(objectMap.get("WEEK"));
            // 类目对应的柱状图
            bar.data(objectMap.get("TOTAL"));
            line.data(objectMap.get("TOTAL"));
        }

        //从数据库取值
//        for(Map<String, Object> objMap: queryList){
//            // 设置类目
//            category.data(objMap.get("WEEK"));
////            // 类目对应的柱状图
//            bar.data(objMap.get("TOTAL"));
//            line.data(objMap.get("TOTAL"));
//        }

//		设置横轴为值轴
        option.xAxis(category);
//        option.legend().orient().x("(function(){return document.getElementById('main').offsetWidth / 2;})()");
//        option.legend().orient(Orient.vertical)
//                .x("(function(){return document.getElementById('main').offsetWidth / 2;})()")
//                .y(56)
//                .itemGap(12)
//                .data("68%的人表示过的不错", "29%的人表示生活压力很大", "3%的人表示“我姓曾”");
//		设置纵轴为类目轴
        option.yAxis(new ValueAxis().boundaryGap(0d, 0.01));

        //设置series数据
        option.series(bar,line);


        // 由于药品名字过长，图表距离左侧距离设置180，关于grid可以看ECharts的官方文档
        option.grid().x(40);

        // 返回Option
        return option;

    }


    public Option getLoginPie(){
        Option option = new Option();

        Title title = new Title();
        title.setText("某站点用户访问来源");
        title.setSubtext("纯属虚构");
        title.setX("center");
        option.setTitle(title);

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger(Trigger.item);
        tooltip.setFormatter("{a} <br/>{b} : {c} ({d}%)");
        option.setTooltip(tooltip);

        Legend legend = new Legend();
        legend.setOrient(Orient.vertical);
        legend.setLeft("left");
        List<String> sList = new ArrayList<>();
        sList.add("直接访问");
        sList.add("邮件营销");
        sList.add("联盟广告");
        sList.add("视频广告");
        sList.add("搜索引擎");
        option.setLegend(legend);

        Pie pie= new Pie();
        pie.setName("访问来源");
        pie.setType(SeriesType.pie);
        pie.setRadius("55%");
        String[] str=new String[2];
        str[0] = "50%";
        str[1] = "60%";
        pie.setCenter(str);
        List<Map<String,String>> dataList = new ArrayList<>();
        Random random=new Random();
        for(int i=0; i<sList.size(); i++){
            Map<String,String> map =new HashMap<>();
            map.put("value",String.valueOf(random.nextInt(1000)));
            map.put("name",sList.get(i));
            dataList.add(map);
        }
        pie.setData(dataList);

        Normal normal=new Normal();
        Label label = new Label();
        label.setShow(true);
        label.formatter("{b}\n{c}\n{d}%");
        normal.setLabel(label);


        ItemStyle itemStyle = new ItemStyle();
        itemStyle.setNormal(normal);
        itemStyle.emphasis().setShadowBlur(10);
        itemStyle.emphasis().setShadowOffsetX(0);
        itemStyle.emphasis().setShadowColor("rgba(0, 0, 0, 0.5)");
        pie.setItemStyle(itemStyle);

        option.series(pie);


        return option;
    }

}
